#include "SerialPort.h"

// Specify the MSWindows naming scheme (COM1, COM2, ...)
#define _TTY_WIN_
#include "qextserialbase.h"
#include "qextserialport.h"
#include <QMessageBox>
#include <QString>

SerialPort::SerialPort(QWidget *parent) : QDialog(parent)
{
	setupUi(this);
        serial=new QextSerialPort();

        autoDetect();
        timer=new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(checkPort()));
}

void SerialPort::autoDetect()
{
    serial->setDataBits((DataBitsType)(chooseDataBits->currentIndex()));
    serial->setParity((ParityType)(chooseParity->currentIndex()));
    serial->setStopBits((StopBitsType)(chooseStopBits->currentIndex()));
    serial->setFlowControl((FlowType)(chooseFlowControl->currentIndex()));
    serial->setBaudRate(BAUD9600);

    for(int i=0;i<10;i++)
    {
        QString s = "COM", num;
        s += num.setNum(i);

        serial->setPortName(s);
        if(serial->open(QextSerialPort::ReadWrite))
        {
            choosePort->addItem(s);
            serial->close();
        }
    }
}

void SerialPort::on_openButton_clicked()
{
	// Make sure things are clear
	serial->close();
	emit(portClosed());
	// read in all the settings and open the port
        if(choosePort->currentText().isEmpty())
        {
            serial->setPortName(choosePort->currentText());
            switch(chooseBaudRate->currentIndex())
            {
                    case 0:
                            serial->setBaudRate(BAUD9600);
                            break;
                    case 1:
                            serial->setBaudRate(BAUD19200);
                            break;
                    case 2:
                            serial->setBaudRate(BAUD38400);
                            break;
                    case 3:
                            serial->setBaudRate(BAUD57600);
                            break;
                    case 4:
                            serial->setBaudRate(BAUD115200);
            }
            serial->setDataBits((DataBitsType)(chooseDataBits->currentIndex()));
            serial->setParity((ParityType)(chooseParity->currentIndex()));
            serial->setStopBits((StopBitsType)(chooseStopBits->currentIndex()));
            serial->setFlowControl((FlowType)(chooseFlowControl->currentIndex()));
            serial->setTimeout(100);
            if(serial->open(QextSerialPort::ReadWrite))
            {
                char dataRead[2];
                char dummy[1] = {CMD_CONNECT};

                timer->start();
                send(1,dummy);
                receive(2,dataRead);
                if(dataRead[0] == CMD_CONNECT || dataRead[1] == ACK)
                    emit(portOpened());
                else
                    emit(portFailed());
            }
            else
            {
                emit(portFailed());
            }
        }
        else
            emit(portFailed());
}

void SerialPort::closePort()
{
	serial->close();
        timer->stop();
	emit(portClosed());
}

void SerialPort::send(int count, char *bytes)
{
        serial->write(bytes, count);
	serial->flush();
}

// read in up to count bytes
// return the number of bytes read
int SerialPort::receive(int count, char *bytes)
{
        return serial->read(bytes, count);
}

void SerialPort::checkPort()
{
	if(serial->bytesAvailable()>0)
	{
            emit(dataReady());
	}
}

void SerialPort::on_cancelButton_clicked()
{
    this->close();
}
